package com.cmsz.upay.service;

import com.cmsz.upay.constant.BusinessTypeConstant;
import com.cmsz.upay.constant.OnLineChargingColumnEnum;
import com.cmsz.upay.constant.PayTradeFlowColumnEnum;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by le on 2017/12/18.
 */
public class HiveServiceImpl implements IHiveService {

    private static final Logger logger = LoggerFactory.getLogger(HiveServiceImpl.class);

    private final static String XML_PREX="STRMTABLE_T";


    public List<ObjectInspector> buildColumnOIS(String type) {
        List<String> columnNames= new ArrayList<String>();
        Map<String,String> columnField= new HashMap<String,String>();
        /*获取列明及属性*/
        if(type.equals(BusinessTypeConstant.TYPE.ONLINE_CHARGE)){
            columnNames= OnLineChargingColumnEnum.getAllColumnName();
            columnField=OnLineChargingColumnEnum.getAllColumnField();
        }else if(type.equals(BusinessTypeConstant.TYPE.PAY_TRADE)){
            columnNames=PayTradeFlowColumnEnum.getAllColumnName();
            columnField=PayTradeFlowColumnEnum.getAllColumnField();
        }
        /*组装类及属性*/
        StringBuffer columnTypeProp=new StringBuffer();
        for(String columnName:columnNames){
            columnTypeProp.append(columnField.get(columnName)).append(":");
        }
        List<TypeInfo> columnTypes = TypeInfoUtils.getTypeInfosFromTypeString(columnTypeProp.toString());
        List<ObjectInspector> columnOIs = new ArrayList<ObjectInspector>();
        ObjectInspector oi;
        for (int c = 0; c < columnNames.size(); c++) {
            oi = TypeInfoUtils.getStandardJavaObjectInspectorFromTypeInfo(columnTypes.get(c));
            columnOIs.add(oi);
        }
        return columnOIs;
    }

    public List<Object> parsingPayTradeXML(String xmlStr) {
        List<Object> cloumnValue=new ArrayList<Object>();
        /*判断输入报文是否满足已<STRMTABLE_T>开头*/
        Pattern pattern = Pattern.compile("(<"+XML_PREX+">)(.*)(</"+XML_PREX+">)");
        Matcher matcher = pattern.matcher(xmlStr);
        if(matcher.matches()){
            try {
                Document document = DocumentHelper.parseText(xmlStr);
                //获取根节点元素对象
                Element root = document.getRootElement();
                //首先获取当前节点的所有属性节点
                // 迭代输出
                for (Iterator iter = root.elementIterator(); iter.hasNext();)
                {
                    Element e = (Element) iter.next();
                    cloumnValue.add(StringUtils.defaultString(e.getText(),""));
                }
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            return cloumnValue;
        }else{
            return null;
        }
    }

    @Override
    public List<Object> parsingOnlineChargingXML(String xmlStr) {
        return null;
    }
}
